﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pout_Porri.Screens.Interface;
using Pout_Porri.Objects;
using Pout_Porri.Objects.Bosses;
using Pout_Porri.Utils;
using Pout_Porri.Levels;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Storage;

namespace Pout_Porri.Screens
{
    class PitfallScreen : Screen, IScreen
    {
       public Texture2D backGround;  
       private Mario mario;
       private Rope rope;
       private Flower flower;
       private Wall wall1, wall2;

        /// <summary>
        /// Especifica quantas telas do pitfall existem
        /// </summary>
       enum Telas { TELA_01, TELA_02, TELA_03, TELA_04, TELA_05, TELA_06 };

        //TO DO implementar esta facilidade do enum de telas, problema: levelManager mal-implementado

        /// <summary>
        /// Especifica em qual tela do pitfall o herói está
        /// </summary>
       Telas tela_atual = Telas.TELA_01;
        
        /// <summary>
        /// A metade de cima do lago
        /// </summary>
       private Lake lakeUp;
       
       /// <summary>
       /// A metade de baixo do lago
       /// </summary>
       private Lake lakeDown;

        /// <summary>
        /// A metade de cima do lago que aumenta e diminui
        /// </summary>
       private MagicLake magicLakeUp;
           
        /// <summary>
       /// A metade de baixo do lago que aumenta e diminui
        /// </summary>
       private MagicLake magicLakeDown;
      
       public LevelManager levelManager;
       private Level level;
       private BarHole barraHole;
       private Turtle turtle1, turtle2, turtle3;
       public int startPosition = 1;
       public bool startSize = true;
       private HedgeHog hedgeHog;
       private Helmet helmet1;
       public BigGhost bigGhost;
       public Pow pow;

        // Atenção: Ao término da fase, classificar a boleana abaixo chamada de levelEnd como verdadeira, assim, a função Change Level no final da classe será acionada.


        private bool levelEnd;
        public bool LevelEnd
        {
            get { return levelEnd; }
            set { LevelEnd = value; }
        }

        public PitfallScreen(Texture2D background, GameWindow gameWindow)
            : base(background)
        {
            this.gameWindow = gameWindow;
            levelManager = new LevelManager();
            levelManager.setLevel(level);
            levelManager.lstLevel.Add(this.level);

            MediaPlayer.Volume = 0.2f;
        }

        public override void load(ContentManager content)
        {
            
            if (levelManager.CurrentLevel == null)
            {
                level = new Level(1, new Vector2(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT), content, background);
                levelManager.setLevel(level);
                Tela1Load(content);
            }
            else
            {
                switch (levelManager.CurrentLevel.getNumLevel())
                {
                    case 1:
                        level = new Level(1, new Vector2(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT), content, background);
                        levelManager.setLevel(level);
                        Tela1Load(content);
                        break;

                    case 102:
                        level = new Level(102, new Vector2(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT), content, background);
                        levelManager.setLevel(level);
                        Tela2Load(content);
                        break;

                    case 103:
                        level = new Level(103, new Vector2(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT), content, background);
                        levelManager.setLevel(level);
                        Tela3Load(content);
                        break;

                    case 104:
                        level = new Level(104, new Vector2(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT), content, background);
                        levelManager.setLevel(level);
                        Tela4Load(content);
                        break;

                    case 105:
                        level = new Level(105, new Vector2(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT), content, background);
                        levelManager.setLevel(level);
                        Tela5Load(content);
                        break;

                    case 106:
                        level = new Level(106, new Vector2(GameConfig.GAME_WIDTH, GameConfig.GAME_HEIGHT), content, background);
                        levelManager.setLevel(level);
                        Tela6Load(content);
                        break;

                    default:
                        Tela1Load(content);
                        break;
                }
            }
        }

        /// <summary>
        /// Load da primeira tela do pitfall
        /// </summary>
        private void Tela1Load(ContentManager content)
        {
            backGround = content.Load<Texture2D>("Images\\Mario\\trees");

            Texture2D barraHoleImg = content.Load<Texture2D>("Images\\Mario\\barra");
            barraHole = new BarHole(barraHoleImg, new Vector2(352, 327), Vector2.Zero, 0.5f);
            level.addGameEntity(barraHole);

            Texture2D marioImg = content.Load<Texture2D>("Images\\Mario\\MarioSpriteSheet");
            if (startPosition == 1)
            { mario = new Mario(marioImg, new Vector2(-50, 220), new Vector2(2, 1)); }
            if (startPosition == 2)
            { mario = new Mario(marioImg, new Vector2(-50, 400), new Vector2(2, 1)); }
            if (startPosition == 3)
            { mario = new Mario(marioImg, new Vector2(700, 220), new Vector2(2, 1)); }
            if (startPosition == 4)
            { mario = new Mario(marioImg, new Vector2(700, 400), new Vector2(2, 1)); }

            if (startSize == true)
                mario.IsGrown = true;
            if (startSize == false)
                mario.IsGrown = false; 
            
            mario.Mass = 1f;
            mario.AccelerationX = 0.05f;
            mario.MaxVelocityX = 5;

            level.setPlayer(mario);

            flower = new Flower(marioImg, new Vector2(520, 240), new Vector2(0, 0));
            flower.zOrder = 0;

            Texture2D wallImg = content.Load<Texture2D>("Images\\Mario\\wall");
            wall1 = new Wall(wallImg, new Vector2(650, 352), Vector2.Zero, "wall1");
            wall1.zOrder = 0.5f;
            level.addGameEntity(wall1);

            wall2 = new Wall(wallImg, new Vector2(650, 413), Vector2.Zero, "wall2");
            wall2.zOrder = 0.8f;
            level.addGameEntity(wall2);

            level.addGameEntity(flower);

            Tileset tileset = new Tileset(14, 2, 0);
            tileset.loadTexture("Images\\Scene\\Pitfall-tileset2", content);

            tileset.setEmptyTile(1);
            tileset.setEmptyTile(12);

            tileset.setHoleTile(3);
            tileset.setHoleTile(14);

            tileset.setLadderTile(5);

            tileset.setFisrstStepLadderTile(7);

            level.setTileset(tileset);

            mario.zOrder = 0;
        }

        /// <summary>
        /// Load da segunda tela do pitfall
        /// </summary>
        private void Tela2Load(ContentManager content)
        {
            backGround = content.Load<Texture2D>("Images\\Mario\\trees");
            Texture2D barraHoleImg = content.Load<Texture2D>("Images\\Mario\\barra");
            barraHole = new BarHole(barraHoleImg, new Vector2(352, 327), Vector2.Zero, 0.5f);
            level.addGameEntity(barraHole);

            BarHole barraHole2 = new BarHole(barraHoleImg, new Vector2(160, 328), Vector2.Zero, 0.5f);
            BarHole barraHole3 = new BarHole(barraHoleImg, new Vector2(158 + barraHoleImg.Width, 328), Vector2.Zero, 0.5f);

            level.addGameEntity(barraHole2);
            level.addGameEntity(barraHole3);

            Texture2D marioImg = content.Load<Texture2D>("Images\\Mario\\MarioSpriteSheet");

            helmet1 = new Helmet(marioImg, new Vector2(690, 200), new Vector2(3, 0));
            helmet1.MyLevel = level;
            level.addGameEntity(helmet1);

            helmet2 = new Helmet(marioImg, new Vector2(800, 200), new Vector2(3, 0));
            helmet2.MyLevel = level;
            level.addGameEntity(helmet2);

            if (startPosition == 1)
            { mario = new Mario(marioImg, new Vector2(50, 220), new Vector2(2, 1)); }
            if (startPosition == 2)
            { mario = new Mario(marioImg, new Vector2(50, 400), new Vector2(2, 1)); }
            if (startPosition == 3)
            { mario = new Mario(marioImg, new Vector2(700, 220), new Vector2(2, 1)); }
            if (startPosition == 4)
            { mario = new Mario(marioImg, new Vector2(700, 400), new Vector2(2, 1)); }

            if (startSize == true)
                mario.IsGrown = true;
            if (startSize == false)
                mario.IsGrown = false;

            mario.Mass = 1f;
            mario.AccelerationX = 0.05f;
            mario.MaxVelocityX = 5;

            level.setPlayer(mario);

           

            Tileset tileset = new Tileset(14, 2, 0);
            tileset.loadTexture("Images\\Scene\\Pitfall-tileset2", content);

            tileset.setEmptyTile(1);
            tileset.setEmptyTile(12);

            tileset.setHoleTile(3);
            tileset.setHoleTile(11);
            tileset.setHoleTile(14);

            tileset.setLadderTile(5);

            tileset.setFisrstStepLadderTile(7);

            level.setTileset(tileset);

            levelManager.setLevel(level);
            levelManager.lstLevel.Add(this.level);
        }

        /// <summary>
        /// Load da terceira tela do pitfall
        /// </summary>
        private void Tela3Load(ContentManager content)
        {
            backGround = content.Load<Texture2D>("Images\\Mario\\trees");


            Texture2D marioImg = content.Load<Texture2D>("Images\\Mario\\MarioSpriteSheet");
            if (startPosition == 1)
            { mario = new Mario(marioImg, new Vector2(50, 220), new Vector2(2, 1)); }
            if (startPosition == 2)
            { mario = new Mario(marioImg, new Vector2(50, 400), new Vector2(2, 1)); }
            if (startPosition == 3)
            { mario = new Mario(marioImg, new Vector2(700, 220), new Vector2(2, 1)); }
            if (startPosition == 4)
            { mario = new Mario(marioImg, new Vector2(700, 400), new Vector2(2, 1)); }

            if (startSize == true)
                mario.IsGrown = true;
            if (startSize == false)
                mario.IsGrown = false;

            mario.Mass = 1f;
            mario.AccelerationX = 0.05f;
            mario.MaxVelocityX = 5;

            level.setPlayer(mario);

            lakeUp = new Lake(marioImg, new Vector2(300, 310), new Vector2(0, 0), true, 0.8f);
            lakeDown = new Lake(marioImg, new Vector2(300, 230), new Vector2(0, 0), false, 0.8f);
            lakeUp.Offset = new Vector2(-100, 0);
            lakeDown.Offset = new Vector2(-100, 0);
            level.addGameEntity(lakeUp);
            level.addGameEntity(lakeDown);

            Texture2D ropeImg = content.Load<Texture2D>("Images\\Mario\\rope");
            rope = new Rope(ropeImg, new Vector2(420, -240), new Vector2(2, 2), 0.5f);
            level.addGameEntity(rope);
            hedgeHog = new HedgeHog(marioImg, new Vector2(200, 400), new Vector2(1, 0));
            level.addGameEntity(hedgeHog);

            hedgeHog.MyLevel = level;


            Tileset tileset = new Tileset(14, 2, 0);
            tileset.loadTexture("Images\\Scene\\Pitfall-tileset2", content);

            tileset.setEmptyTile(1);
            tileset.setEmptyTile(12);

            tileset.setHoleTile(3);
            tileset.setHoleTile(11);
            tileset.setHoleTile(14);

            tileset.setLadderTile(5);

            tileset.setFisrstStepLadderTile(7);

            level.setTileset(tileset);

            levelManager.setLevel(level);
            levelManager.lstLevel.Add(this.level);

            mario.zOrder = 0;
        }

        /// <summary>
        /// Load da quarta tela do pitfall
        /// </summary>
        private void Tela4Load(ContentManager content)
        {
            backGround = content.Load<Texture2D>("Images\\Mario\\trees");


            Texture2D marioImg = content.Load<Texture2D>("Images\\Mario\\MarioSpriteSheet");
            if (startPosition == 1)
            { mario = new Mario(marioImg, new Vector2(50, 220), new Vector2(2, 1)); }
            if (startPosition == 2)
            { mario = new Mario(marioImg, new Vector2(50, 400), new Vector2(2, 1)); }
            if (startPosition == 3)
            { mario = new Mario(marioImg, new Vector2(700, 220), new Vector2(2, 1)); }
            if (startPosition == 4)
            { mario = new Mario(marioImg, new Vector2(700, 400), new Vector2(2, 1)); }

            if (startSize == true)
                mario.IsGrown = true;
            if (startSize == false)
                mario.IsGrown = false;
            
            mario.Mass = 1f;
            mario.AccelerationX = 0.05f;
            mario.MaxVelocityX = 5;

            level.setPlayer(mario);

            lakeUp = new Lake(marioImg, new Vector2(300, 310), new Vector2(0, 0), true, 0.8f);
            lakeDown = new Lake(marioImg, new Vector2(300, 230), new Vector2(0, 0), false, 0.8f);
            
            lakeUp.Offset = new Vector2(-100, 0);
            lakeDown.Offset = new Vector2(-100, 0);
            level.addGameEntity(lakeUp);
            level.addGameEntity(lakeDown);

            turtle1 = new Turtle(marioImg, new Vector2(240, 230), new Vector2(0, 0), "turtle1");
            level.addGameEntity(turtle1);

            turtle2 = new Turtle(marioImg, new Vector2(330, 230), new Vector2(0, 0), "turtle2");
            level.addGameEntity(turtle2);

            turtle3 = new Turtle(marioImg, new Vector2(420, 230), new Vector2(0, 0), "turtle3");
            level.addGameEntity(turtle3);

            Tileset tileset = new Tileset(14, 2, 0);
            tileset.loadTexture("Images\\Scene\\Pitfall-tileset2", content);

            tileset.setEmptyTile(1);
            tileset.setEmptyTile(12);

            tileset.setHoleTile(3);
            tileset.setHoleTile(11);
            tileset.setHoleTile(14);

            tileset.setLadderTile(5);

            tileset.setFisrstStepLadderTile(7);

            level.setTileset(tileset);

            levelManager.setLevel(level);
            levelManager.lstLevel.Add(this.level);

            mario.zOrder = 0;
        }

        /// <summary>
        /// Load da quinta tela do pitfall
        /// </summary>
        private void Tela5Load(ContentManager content)
        {
            backGround = content.Load<Texture2D>("Images\\Mario\\trees");


            Texture2D marioImg = content.Load<Texture2D>("Images\\Mario\\MarioSpriteSheet");
            if (startPosition == 1)
            { mario = new Mario(marioImg, new Vector2(50, 220), new Vector2(2, 1)); }
            if (startPosition == 2)
            { mario = new Mario(marioImg, new Vector2(50, 400), new Vector2(2, 1)); }
            if (startPosition == 3)
            { mario = new Mario(marioImg, new Vector2(700, 220), new Vector2(2, 1)); }
            if (startPosition == 4)
            { mario = new Mario(marioImg, new Vector2(700, 400), new Vector2(2, 1)); }

            if (startSize == true)
                mario.IsGrown = true;
            if (startSize == false)
                mario.IsGrown = false;

            mario.Mass = 1f;
            mario.AccelerationX = 0.05f;
            mario.MaxVelocityX = 5;

            level.setPlayer(mario);

            Texture2D lakeImg = Utils.Util.CreatePartImage(new Rectangle(0,336, 336, 84), marioImg);
            magicLakeUp = new MagicLake(lakeImg, new Vector2(380, 310), new Vector2(0, 0), true, 0.8f);
            magicLakeDown = new MagicLake(lakeImg, new Vector2(380, 230), new Vector2(0, 0), false, 0.8f);
            magicLakeUp.Offset = new Vector2(-100, 0);
            magicLakeDown.Offset = new Vector2(-100, 0);
            level.addGameEntity(magicLakeUp);
            level.addGameEntity(magicLakeDown);
         
            Tileset tileset = new Tileset(14, 2, 0);
            tileset.loadTexture("Images\\Scene\\Pitfall-tileset2", content);

            tileset.setEmptyTile(1);
            tileset.setEmptyTile(12);

            tileset.setHoleTile(3);
            tileset.setHoleTile(11);
            tileset.setHoleTile(14);

            tileset.setLadderTile(5);

            tileset.setFisrstStepLadderTile(7);

            level.setTileset(tileset);

            levelManager.setLevel(level);
            levelManager.lstLevel.Add(this.level);

            mario.zOrder = 0;
        }

        /// <summary>
        /// Load da sexta tela do pitfall
        /// </summary>
        private void Tela6Load(ContentManager content)
        {
            backGround = content.Load<Texture2D>("Images\\Mario\\trees");


            Texture2D marioImg = content.Load<Texture2D>("Images\\Mario\\MarioSpriteSheet");
            if (startPosition == 1)
            { mario = new Mario(marioImg, new Vector2(50, 220), new Vector2(2, 1)); }
            if (startPosition == 2)
            { mario = new Mario(marioImg, new Vector2(50, 400), new Vector2(2, 1)); }
            
            if (startSize == true)
                mario.IsGrown = true;
            if (startSize == false)
                mario.IsGrown = false;

            mario.Mass = 1f;
            mario.AccelerationX = 0.05f;
            mario.MaxVelocityX = 5;

            level.setPlayer(mario);

            bigGhost = new BigGhost(marioImg,new Vector2(600,200), new Vector2(1,1));
            bigGhost.MyLevel = level;
            bigGhost.Mass = 0f;
            bigGhost.scale = 0.8f;
            level.addGameEntity(bigGhost);

            pow = new Pow(marioImg, new Vector2(600, 240), new Vector2(0, 0));

            pow.MyLevel = level;
            pow.Mass = 0f;
            level.addGameEntity(pow);
            
            Tileset tileset = new Tileset(14, 2, 0);
            tileset.loadTexture("Images\\Scene\\Pitfall-tileset2", content);

            tileset.setEmptyTile(1);
            tileset.setEmptyTile(12);

            tileset.setHoleTile(3);
            tileset.setHoleTile(11);
            tileset.setHoleTile(14);

            tileset.setLadderTile(5);

            tileset.setFisrstStepLadderTile(7);

            level.setTileset(tileset);

            levelManager.setLevel(level);
            levelManager.lstLevel.Add(this.level);

            mario.zOrder = 0;
        }

        public void deleteGameEntities()
        {
            for (int i = 0; i < World.gameEntitys.Count;i++ )
            {
                if (!World.gameEntitys[i].isVisible())
                {
                    World.gameEntitys.Remove(World.gameEntitys[i]);
                    break;
                }
            }
        }

        public override void update(GameTime gameTime, GameWindow Window)
        {
            deleteGameEntities();
            
            if (mario.IsBoundaryRight)
            {
                if (mario.IsGrown == true)
                    startSize = true;

                else if (mario.IsGrown == false)
                    startSize = false;
                
                if (mario.position.Y <= 250)
                    startPosition = 1;

                else if (mario.position.Y > 250)
                    startPosition = 2;

                switch (levelManager.CurrentLevel.getNumLevel())
                {
                    case 1:
                        level.setNumLevel(102);
                        load(ScreenManager.Content);
                        break;

                    case 102:
                        level.setNumLevel(103);
                        load(ScreenManager.Content);
                        break;

                    case 103:
                        level.setNumLevel(104);
                        load(ScreenManager.Content);
                        break;

                    case 104:
                        level.setNumLevel(105);
                        load(ScreenManager.Content);
                        break;

                    case 105:
                        level.setNumLevel(106);
                        load(ScreenManager.Content);
                        
                      
                        break;
                }
            }
            else if (mario.IsBoundaryLeft)
            {
                if (mario.IsGrown == true)
                    startSize = true;

                else if (mario.IsGrown == false)
                    startSize = false; 
                
                if (mario.position.Y <= 250)
                    startPosition = 3;

                else if (mario.position.Y > 250)
                    startPosition = 4; 
                
                switch (levelManager.CurrentLevel.getNumLevel())
                {
                    case 102:
                        level.setNumLevel(1);
                        load(ScreenManager.Content);
                        break;

                    case 103:
                        level.setNumLevel(102);
                        load(ScreenManager.Content);
                        break;

                    case 104:
                        level.setNumLevel(103);
                        load(ScreenManager.Content);
                        break;

                    case 105:
                        level.setNumLevel(104);
                        load(ScreenManager.Content);
                        break;
                }
            }
            if (bigGhost != null)
            {

                if (World.entityCollided is Pow)
                {
                    if (bigGhost.currentState == EnemyStates.stand && pow.currentState == EnemyStates.stand)
                    {
                        bigGhost.currentState = EnemyStates.running;
                        pow.currentState = EnemyStates.dying;
                    }
                }

                if (World.entityCollided is BigGhost)
                {
                    if (bigGhost.currentState == EnemyStates.stand)
                    {
                        if (mario.IsGrown)
                            mario.IsGrown = false;
                    }

                    if (bigGhost.currentState == EnemyStates.running)
                    {
                        bigGhost.currentState = EnemyStates.dying;
                    }
                }
            }
            levelManager.updateLevel(gameTime, Game1.keyboardState);
        }

        public override void draw(SpriteBatch spriteBatch)
        {
            Game1.camera.Position = new Vector2(400, 280);
            spriteBatch.Draw(backGround, new Rectangle(0, -35, Game1.graphics.GraphicsDevice.Viewport.Width, 327), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 0.7f);
            levelManager.drawCurrentLevel(spriteBatch);
        }

    }
}
